import {Component, OnInit} from '@angular/core';
import {Globals} from '../../globals';
import {FormBuilder, FormGroup} from '@angular/forms';
import {ApiUrl} from '../../config/api-url';
import {BaseService} from '../../shared/service/base.service';
import {PageHelper} from '../../utils/page-helper-model';
import {ActivatedRoute, Router} from '@angular/router';
import {UserInfoModel} from '../../models/user-info.model';
import {Consts} from '../../config/consts';
import {MyDateModel} from '../../models/my-date.model';
import {RoleIdEnum} from '../../enums/role-id.enum';

@Component({
  selector: 'app-authorized-apply-list',
  templateUrl: './ledger-order.component.html',
  styleUrls: ['./ledger-order.component.less']
})
export class LedgerOrderComponent implements OnInit {
  roleIdEnum = {
    SUPER_ADMINISTRATOR: RoleIdEnum.SUPER_ADMINISTRATOR,
    AGENT: RoleIdEnum.AGENT,
    MERCHANT: RoleIdEnum.MERCHANT,
  };
  isMobile = Globals.isMobile();
  listOfData: any;
  // 分页
  pageHelper: PageHelper = new PageHelper;
  formModel: FormGroup;
  // 用户信息
  userInfo: UserInfoModel;
  // 运营商
  userList: [];
  // 门店
  merchantStoreList: [];
  // 开始日期类型
  startDateTimeType = 'start';
  // 结束日期类型
  endDateTimeType = 'end';
  // 开始日期
  startDateTime: Date;
  // 结束日期
  endDateTime: Date;

  stateLedgerTotal: 0;
  stateTippingTotal: 0;
  constructor(private fb: FormBuilder, private baseService: BaseService, private activatedRoute: ActivatedRoute, private router: Router) {
    // this.listOfData = USER;
    this.buildForm();
  }

  ngOnInit(): void {
    this.userInfo = JSON.parse(sessionStorage.getItem(Consts.USER_INFO));
    this.startDateTime = this.baseService.zorro.getDateTime().startToday;
    this.endDateTime = this.baseService.zorro.getDateTime().endToday;
    this.formModel.get('startDate').setValue(this.startDateTime);
    this.formModel.get('endDate').setValue(this.endDateTime);
    this.findList();
    this.findStat();
    this.findAllMerchantStoreByUserInfo();
    this.getAllUserOfName();
    // this.findMerchantStatistic();
  }

  buildForm() {
    this.formModel = this.fb.group({
      outTradeNo: [null, []],
      merchantStoreId: ['0', []],
      userId: ['', []],
      startDate: [null, []],
      endDate: [null, []],
      orderType: ['0', []],
      ledgerType: ['0', []],
      merchantShortId: [null, []],
      status: ['-1', []],
    });
  }
  onPageSizeChange(pageSize: any){
    this.pageHelper.pageSize = pageSize;
    this.pageHelper.currentPage = 1;
    this.findList();
  }
  findList() {
    this.baseService.findListByMuchCondition(ApiUrl.LedgerOrder + '/list', this.formModel.value, this.pageHelper).subscribe(
      data => {
        if (data.status == 0) {
          this.pageHelper.totalItems = data.data.total;
          this.listOfData = data.data.list;
          console.log(this.listOfData);
        } else {
          this.baseService.layer.messageError(data.msg);
        }
      }
    );
  }

  submitForm() {
    this.pageHelper.currentPage = 1;
    this.formModel.get('startDate').setValue(this.startDateTime);
    this.formModel.get('endDate').setValue(this.endDateTime);
    this.findList();
    this.findStat();
  }

  nzPageIndexChange($event: number) {
    this.pageHelper.currentPage = $event;
    this.findList();
  }


  gainUserId($event: any) {
    this.findAllMerchantStoreByUserInfo();
  }


  getAllUserOfName() {
    this.baseService.get(ApiUrl.UserUrl + '/getAllUserOfName?agencyId=0').subscribe(data => {
      if (data.status == 0) {
        this.userList = data.data;
      } else {
        this.baseService.layer.messageError(data.msg);
      }
    });
  }
  findAllMerchantStoreByUserInfo() {
    let userId = null;
    if (this.formModel.get('userId').value != null) {
      userId = this.formModel.get('userId').value;
    } else {
      userId = '';
    }
    this.baseService.get(ApiUrl.MerchantStoreUrl + '/findAllMerchantStoreByUserInfo?userId=' + userId).subscribe(
      data => {
        if (data.status == 0) {
          this.merchantStoreList = data.data;
        } else {
          this.baseService.layer.messageError(data.msg);
        }
      }
    );
  }
  // 将开始、结束时间往外抛
  outDateTime($event: MyDateModel) {
    /**
     * date: Fri Mar 19 2021 23:59:06 GMT+0800 (中国标准时间) {}
     * type: "end"
     */
    if ($event.type == 'start') {
      this.startDateTime = $event.date;
    } else if ($event.type == 'end') {
      this.endDateTime = $event.date;
    }
  }
  findStat() {
    this.baseService.findListByMuchCondition(ApiUrl.LedgerOrder + '/findStat', this.formModel.value).subscribe(
      data => {
        if (data.status == 0) {
          console.log(data)
          this.stateLedgerTotal = data.data.stateLedgerTotal;
          this.stateTippingTotal = data.data.stateTippingTotal;
        } else {
          this.baseService.layer.messageError(data.msg);
        }
      }
    );
  }
}
